package com.dhcc.bpm.modules.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.system.service.ILogErrorService;
import com.dhcc.bpm.modules.system.vo.OpcLogErrorVO;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.log.model.LogError;
import org.springblade.core.log.model.LogErrorVo;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName LogErrorController
 * @Description 错误日志控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@ApiIgnore
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_LOG_NAME + "/error")
public class LogErrorController {
	private  final static Logger log = LoggerFactory.getLogger(LogErrorController.class);
	private ILogErrorService errorLogService;

	/**
	 * @Description 查询单条
	 * @Author sjm
	 * @Date 10:03 2020/2/22 0022
	 * @param logError
	 * @return org.springblade.core.tool.api.R<org.springblade.core.log.model.LogError>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "查询单条", notes = "传入log")
	@ApiLog("查询错误日志详情")
	@Log(title = "查询错误日志详情", businessType = BusinessType.SELECT)
	public R<LogError> detail(LogError logError) {
		return R.data(errorLogService.getOne(Condition.getQueryWrapper(logError)));
	}

	/**
	 * @Description 查询多条(分页)
	 * @Author sjm
	 * @Date 10:04 2020/2/22 0022
	 * @param logError
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<org.springblade.core.log.model.LogErrorVo>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "查询多条(分页)", notes = "传入log")
	@ApiLog("分页查询错误日志")
	@Log(title = "查询错误日志", businessType = BusinessType.SELECT)
	public R<IPage<LogErrorVo>> list(@ApiIgnore @RequestParam Map<String, Object> logError, Query query) {
		IPage<LogError> pages = errorLogService.page(Condition.getPage(query.setDescs("create_time")), Condition.getQueryWrapper(logError, LogError.class));
		List<LogErrorVo> records = pages.getRecords().stream().map(logApi -> {
			LogErrorVo vo = BeanUtil.copy(logApi, LogErrorVo.class);
			vo.setStrId(Func.toStr(logApi.getId()));
			return vo;
		}).collect(Collectors.toList());
		IPage<LogErrorVo> pageVo = new Page<>(pages.getCurrent(), pages.getSize(), pages.getTotal());
		pageVo.setRecords(records);
		return R.data(pageVo);
	}

	/**
	 * @Description 查询多条(分页)
	 * @Author sjm
	 * @Date 10:04 2020/2/22 0022
	 * @param logError
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.system.vo.OpcLogErrorVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "查询多条(分页)", notes = "传入logError")
	@ApiLog("分页查询错误日志")
	@Log(title = "查询错误日志", businessType = BusinessType.SELECT)
	public R<IPage<OpcLogErrorVO>> list(OpcLogErrorVO logError, Query query) {
		try {
			if (logError.getLogDate() != null && logError.getLogDate().length() > 0) {
				String startDate = logError.getLogDate().split(",")[0];
				String endDate = logError.getLogDate().split(",")[1];
				logError.setStartDate(startDate);
				logError.setEndDate(endDate);
			}
			IPage<OpcLogErrorVO> pages = errorLogService.selectLogErrorPage(Condition.getPage(query), logError);
			return R.data(pages);
		}catch (Exception e) {
			log.error("日期转换异常：", e);
			return R.data(null);
		}
	}

}
